//
//  SalesLeadViewController.m
//  CareWithFriends
//
//  Created by Versatile Systems, Inc on 11/19/13.
//  Copyright (c) 2013 Muludiang. All rights reserved.
//

#import "SalesLeadViewController.h"
#import "CWFData.h"

@interface SalesLeadViewController ()

@end

@implementation SalesLeadViewController

@synthesize submitLeadBtn;
@synthesize salesText;
@synthesize hideKeyboardBtn;
@synthesize customerName, contactNumber;
@synthesize preferredContact;

-(IBAction)hideKeyboard:(id)sender{
    [salesText resignFirstResponder];
    [customerName resignFirstResponder];
    [contactNumber resignFirstResponder];
    [preferredContact resignFirstResponder];
    
}

-(IBAction)submitLead:(id)sender{
    UIAlertView *submitAlert;
    if( ([[[customerName text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[contactNumber text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[preferredContact text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0) || ([[[salesText text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length ] == 0)){
        submitAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Please fill out all fields" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil ];
        [submitAlert setTag:1];
    } else{
        submitAlert = [[UIAlertView alloc] initWithTitle:nil message:@"Sales Lead Submitted" delegate:self cancelButtonTitle:@"Home" otherButtonTitles:nil ];
        [submitAlert setTag:2];
    }
    [submitAlert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 2){
        NSString *email;
    NSString *mobile;
    NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet]invertedSet];
    if ([preferredContact.text rangeOfCharacterFromSet:notDigits].location == NSNotFound){
        email = @"";
        mobile = preferredContact.text;
    }
    else{
        email = preferredContact.text;
        mobile = @"";
    }
    [[CWFData sharedInstance] createSale:customerName.text customerPhone:contactNumber.text custEmail:email custMobilePhone:mobile description:salesText.text];
    [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:placeholderText]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor]; //optional
    }
    [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        textView.text = placeholderText;
        textView.textColor = [UIColor lightGrayColor]; //optional
    }
    [textView resignFirstResponder];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    [toolbar setBarStyle:UIBarStyleBlackTranslucent];
    [toolbar sizeToFit];
    
    UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hideKeyboard:)];
    
    NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];
    
    [toolbar setItems:itemsArray];
    
    [customerName setInputAccessoryView:toolbar];
    [contactNumber setInputAccessoryView:toolbar];
    [salesText setInputAccessoryView:toolbar];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[self.salesText layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
    [[self.salesText layer] setBorderWidth:1.5];
    [[self.salesText layer] setCornerRadius:5];

    placeholderText = @"What would the customer like to order from Comcast?";
    salesText.text = placeholderText;
    salesText.textColor = [UIColor lightGrayColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
